package cn.cuiqi.dynamic.thread.pool.sdk.trigger.listener;

import cn.cuiqi.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import cn.cuiqi.dynamic.thread.pool.sdk.domain.model.Entity.ThreadPoolConfigEntity;
import cn.cuiqi.dynamic.thread.pool.sdk.registry.IRegistry;
import cn.cuiqi.dynamic.thread.pool.sdk.trigger.job.ThreadPoolReportJob;
import com.alibaba.fastjson.JSON;
import org.redisson.api.listener.MessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: Cuiqi
 * @Description: 动态线程池变更监听
 * @Date: 2024/8/1 23:18
 **/
@Component
public class ThreadPoolConfigListener implements MessageListener<ThreadPoolConfigEntity> {

    private final Logger logger = LoggerFactory.getLogger(ThreadPoolConfigListener.class);

    @Autowired
    private IRegistry registry;
    
    @Autowired
    private IDynamicThreadPoolService dynamicThreadPoolService;
    
    
    @Override
    public void onMessage(CharSequence charSequence, ThreadPoolConfigEntity threadPoolConfigEntity) {
        logger.info("动态线程池，调整线程池配置>>>>线程池名称:{} 核心线程数:{} 最大线程数:{}", threadPoolConfigEntity.getThreadPoolName(), threadPoolConfigEntity.getPoolSize(), threadPoolConfigEntity.getMaximumPoolSize());
        dynamicThreadPoolService.updateThreadPoolConfig(threadPoolConfigEntity);

        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        logger.info(">>>>>>上报修改后的线程池：{}", JSON.toJSONString(threadPoolConfigEntities));
        //更新上报后的数据
        registry.reportThreadPoolList(threadPoolConfigEntities);
        ThreadPoolConfigEntity threadPoolConfigEntityCurrent = dynamicThreadPoolService.queryThreadPoolByBeanName(threadPoolConfigEntity.getThreadPoolName());
        registry.reportThreadPoolParamter(threadPoolConfigEntityCurrent);
        logger.info("动态线程池，上报线程池配置：{}", JSON.toJSONString(threadPoolConfigEntity));
        
    }
    
    
}
